<?php
/**
 * Created by PhpStorm OF yuyi.
 * User: YUYI
 * Date: 2018/6/4 0004
 * Time: 上午 10:07
 * Administrator
 */

namespace app\admin\controller;


use app\common\controller\Base;
use app\common\model\Users;
use app\common\service\UserService;
use app\common\util\PostUtil;
use think\db\Query;
use think\Exception;
use think\Queue;

class User extends Base
{
    protected $getList = 'Users';

    use PostUtil;

    public function index()
    {
        return $this->fetch();
    }

    public function updatePost()
    {
        !$this->request->isPost()?exit($this->errorInfo('非法请求')):'';
        $userModel = new Users();
        $r = $userModel->allowField(true)->update(input(),['userId'=>input('userId')]);
        return $r?$this->successInfo('成功'):$this->errorInfo('失败');
    }

    public function add()
    {
        return $this->fetch();
    }

    public function addUser(){
        $r = Users::create(input())->allowField(true);
        return $r?$this->successInfo('成功'):$this->errorInfo('失败');
    }

    public function center()
    {
        $user = UserService::getCurrentUserInfo();
        $this->assign('user',$user);
        return $this->fetch();
    }

    public function updatePersonInfo()
    {
        $data = [];
        $ps = input('ps');
        if ($ps){
            $data['personalitySignature']=$ps;
        }

        $user = UserService::getCurrentUserInfo();
        $r = Users::update($data,['userId'=>$user['userId']]);
        $user = Users::get(['userId'=>$user['userId']]);
        UserService::updateUserInfo($user);
        if ($r){
            return $this->successInfo("资料修改成功");
        }else{
            return $this->errorInfo('数据保存异常。');
        }
    }

    /**
     * 用户身份证信息
     * @return string
     */
    public function getUserIdcardInfo()
    {
        return $this->errorInfo('调试错误信息');
        $idcard = input('idCardNo');
        $appkey = config('api_config.juhe')['appkey'];
        $url = "http://apis.juhe.cn/idcard/index?key=$appkey&cardno=$idcard";
        try {
            $result = json_decode(self::getVisit($url),1);
        }catch (Exception $exception){
            return $this->exceptionInfo($exception);
        }
        if ($result['resultcode'] == 200){
            return $this->successInfo($result['reason'],$result['result']);
        }
        return $this->errorInfo($result['reason']);
    }

    /**
     * 用户详情页
     * @param $id
     * @return mixed|string
     * @throws \think\exception\DbException
     */
    public function detail($id)
    {
        $user = Users::get($id,'equipments');
        if (!$user){
            return $this->errorInfo('非法访问');
        }
        $this->assign('user',$user);
        return $this->fetch();
    }
}